/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroEmpregado.java
 *
 * Created on Oct 19, 2011, 6:48:13 PM
 */

package manutençãoempregados.GUI;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import manutençãoempregados.UTILS.CadastrarEmpregado;
import classesValidacao.ValidaDadosCadastro;
import classesDados.*;

/**
 *
 * @author Leandro
 */
public class AlteraEmpregadoGUI extends javax.swing.JDialog {

    /** Creates new form CadastroEmpregado */
    public AlteraEmpregadoGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setFalseEnabled(); 
        cpfTxtField.setText("");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SexoButtonGroup = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        AlteraButton = new javax.swing.JButton();
        cpfTxtField = new javax.swing.JTextField();
        nomeTextField = new javax.swing.JTextField();
        dtnascTextField = new javax.swing.JTextField();
        salarioTextField = new javax.swing.JTextField();
        dtadmTextField = new javax.swing.JTextField();
        dtdeslTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        femininoButton = new javax.swing.JRadioButton();
        MasculinoButton = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("Encontre o cpf:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setText("Nome:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setForeground(new java.awt.Color(0, 51, 51));
        jLabel4.setText("Data de Nascimento:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setForeground(new java.awt.Color(0, 51, 51));
        jLabel5.setText("Data de Admissão:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 51));
        jLabel6.setText("Salário Atual:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setForeground(new java.awt.Color(0, 51, 51));
        jLabel7.setText("Data de Desligamento:");

        AlteraButton.setText("Alterar");
        AlteraButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AlteraButtonActionPerformed(evt);
            }
        });

        cpfTxtField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cpfTxtFieldActionPerformed(evt);
            }
        });

        nomeTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nomeTextFieldActionPerformed(evt);
            }
        });

        dtnascTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtnascTextFieldActionPerformed(evt);
            }
        });

        salarioTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salarioTextFieldActionPerformed(evt);
            }
        });

        dtadmTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtadmTextFieldActionPerformed(evt);
            }
        });

        dtdeslTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dtdeslTextFieldActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18));
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Alterar dados do Empregado");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 51, 51));
        jLabel8.setText("Sexo:");

        femininoButton.setText("Feminino");
        femininoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                femininoButtonActionPerformed(evt);
            }
        });

        MasculinoButton.setText("Masculino");
        MasculinoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MasculinoButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(dtadmTextField, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dtnascTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                            .addComponent(salarioTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                            .addComponent(dtdeslTextField, javax.swing.GroupLayout.Alignment.LEADING))
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addContainerGap())
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(jLabel8))
                            .addGap(60, 60, 60)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nomeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                                .addComponent(cpfTxtField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(femininoButton)
                                    .addGap(18, 18, 18)
                                    .addComponent(MasculinoButton))
                                .addComponent(AlteraButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(41, 41, 41)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cpfTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nomeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(femininoButton)
                    .addComponent(MasculinoButton)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dtnascTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(dtadmTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(salarioTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(dtdeslTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(AlteraButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void AlteraButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AlteraButtonActionPerformed
        /*após entrar com todos os dados,eles serão validados e criado o objeto Empr aqui, onde será
            enviado para o CadastrarEmpregado.java e lá ele será colocado no xml*/
    
        if(femininoButton.isSelected()){
            sexo = "Feminino";
        }
        
        if(MasculinoButton.isSelected()){
            sexo = "Masculino";
        }
    
        Date dataDeNascimento = new Date(dataNasc);
        Date dataDeAdmissão = new Date(dataAdm);
        Date dataDeDesligamento = new Date(dataDesl);
        
        
        
        empr = new Empregado(cpf, nome, sexo, dataDeNascimento, dataDeAdmissão, salario);
        cadEmpr.AlterarEmpregado(empr);
        closeWindowOption();        
}//GEN-LAST:event_AlteraButtonActionPerformed

private void cpfTxtFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cpfTxtFieldActionPerformed
      try{
           if(cadEmpr.verificaExistencia(cpfTxtField.getText())){
               empr = new Empregado(cadEmpr.getEmpregado(cpfTxtField.getText()));
               cpf = new CPF (cpfTxtField.getText());
               setSpaces(empr);
               cpfTxtField.setEnabled(false);
               nomeTextField.setEnabled(true);
               nomeTextField.requestFocusInWindow();
              }
           else
               throw new Exception("Não foi encontrado");               
          }    
            catch(Exception ex){
             JOptionPane.showMessageDialog(this,ex.getMessage(), "CPF", JOptionPane.ERROR_MESSAGE);
       }     
}//GEN-LAST:event_cpfTxtFieldActionPerformed

private void nomeTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nomeTextFieldActionPerformed
      try{           
            if(ValidaDadosCadastro.validaNome(nomeTextField.getText()) && nomeTextField.getText().isEmpty() == false){
                 nome = nomeTextField.getText();
                 femininoButton.setEnabled(true);
                 MasculinoButton.setEnabled(true);
                 dtnascTextField.setEnabled(true);
                 dtnascTextField.setEnabled(true);
                  }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,ex.getMessage(), "Nome", JOptionPane.ERROR_MESSAGE);
        }
}//GEN-LAST:event_nomeTextFieldActionPerformed

private void dtnascTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtnascTextFieldActionPerformed
        try{
          if(ValidaDadosCadastro.validaDatanasc(dtnascTextField.getText())){
              dataNasc = dtnascTextField.getText();
              dtadmTextField.setEnabled(true);
              dtadmTextField.requestFocusInWindow();
          }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de Nascimento", JOptionPane.ERROR_MESSAGE);
          }
}//GEN-LAST:event_dtnascTextFieldActionPerformed

private void dtadmTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtadmTextFieldActionPerformed
         try{
          if(ValidaDadosCadastro.validaDataadm(dtadmTextField.getText(),dataNasc)){
              dataAdm = dtadmTextField.getText();
              salarioTextField.setEnabled(true);
              salarioTextField.requestFocusInWindow();
          }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de Admissão", JOptionPane.ERROR_MESSAGE);
          }
}//GEN-LAST:event_dtadmTextFieldActionPerformed

private void salarioTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salarioTextFieldActionPerformed
         try{
             if(Salario.validaSalario(Double.parseDouble(salarioTextField.getText()))){
               salario = new Salario(Double.parseDouble(salarioTextField.getText()));
               dtdeslTextField.setEnabled(true);
               dtdeslTextField.requestFocusInWindow();
             }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Salário", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_salarioTextFieldActionPerformed

private void dtdeslTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dtdeslTextFieldActionPerformed
       try{
             if(ValidaDadosCadastro.validaDataadm(dtdeslTextField.getText(),dataAdm)){
               dataDesl = dtdeslTextField.getText();
               AlteraButton.setEnabled(true);
               AlteraButton.requestFocusInWindow();
             }
          } catch (Exception ex) {
                   JOptionPane.showMessageDialog(this,ex.getMessage(), "Data de desligamento", JOptionPane.ERROR_MESSAGE);
    }  
}//GEN-LAST:event_dtdeslTextFieldActionPerformed

private void femininoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_femininoButtonActionPerformed
        dtnascTextField.setEnabled(true);
        dtnascTextField.requestFocusInWindow();
        MasculinoButton.setSelected(false);
}//GEN-LAST:event_femininoButtonActionPerformed

private void MasculinoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MasculinoButtonActionPerformed
        dtnascTextField.setEnabled(true);
        dtnascTextField.requestFocusInWindow();
        femininoButton.setSelected(false);
}//GEN-LAST:event_MasculinoButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                AlteraEmpregadoGUI dialog = new AlteraEmpregadoGUI(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public void generalFocusGain(JTextField textField){
        textField.setText("");
    }
    
    private void setSpaces(Empregado empr) {
        nomeTextField.setText(empr.getNome());
        
        if("Masculino".equals(empr.getSexo())){
            MasculinoButton.setSelected(true);
        }
        
        if("Feminino".equals(empr.getSexo())){
            femininoButton.setSelected(true);
        }
        
        dtnascTextField.setText(convertData(empr.getDatanasc(),false));
        dtadmTextField.setText(convertData(empr.getDataadm(),false));
        salarioTextField.setText(String.valueOf(empr.getSalario().getSalario()));
        dtdeslTextField.setText(convertData(empr.getDatadesl(),false));
    }
    
    private void closeWindowOption() {
        int response = JOptionPane.showConfirmDialog(this,"Deseja alterar outro empregado?","Usuário alterado com sucesso", JOptionPane.YES_NO_OPTION);
        
        if(response == JOptionPane.YES_OPTION){
            System.out.println("yes");
            setFalseEnabled();
            cpfTxtField.setEnabled(true);
            cpfTxtField.requestFocus();
        }
        
        if(response == JOptionPane.NO_OPTION){
            System.out.println("no");
            setFalseEnabled();
            this.setVisible(false);            
        }
    }
    
    private void setFalseEnabled() {
        nomeTextField.setEnabled(false);
        femininoButton.setEnabled(false);
        MasculinoButton.setEnabled(false);
        dtnascTextField.setEnabled(false);        
        dtadmTextField.setEnabled(false);
        dtdeslTextField.setEnabled(false);        
        salarioTextField.setEnabled(false);
        AlteraButton.setEnabled(false);
    }
    
    private String convertData(Date date,boolean time) {
        
          if (date == null) {
            return "";
        }
        Locale brasil = new Locale("pt", "BR");
        DateFormat df;
        if (time) {
            df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, brasil);
        } else {
            df = DateFormat.getDateInstance(DateFormat.MEDIUM, brasil);
        }
        return df.format(date);
        
    }
    
    private Empregado empr = new Empregado();
    
    private CPF cpf;
    private String nome;
    private String sexo;
    private String dataNasc;
    private String dataAdm;
    private Salario salario = new Salario();
    private String dataDesl;
    private CadastrarEmpregado cadEmpr = new CadastrarEmpregado();
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AlteraButton;
    private javax.swing.JRadioButton MasculinoButton;
    private javax.swing.ButtonGroup SexoButtonGroup;
    private javax.swing.JTextField cpfTxtField;
    private javax.swing.JTextField dtadmTextField;
    private javax.swing.JTextField dtdeslTextField;
    private javax.swing.JTextField dtnascTextField;
    private javax.swing.JRadioButton femininoButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField nomeTextField;
    private javax.swing.JTextField salarioTextField;
    // End of variables declaration//GEN-END:variables

}
